package com.yyk.yykywd.mvvm.model

import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseResponse
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.http.ApiService
import com.yyk.yykywd.mvvm.entity.*
import com.yyk.yykywd.network.BusinessApi
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 文件描述：
 * Created by guo on 2019-11-30.
 */
class CustomerModel {

    fun addHospitalClient(hos_name: String,
                          type_name: String,
                          grade_name: String,
                          call_time: String,
                          call_name: String,
                          call_grade: String,
                          call_phone: String,
                          hos_text: String,
                          flag: String,
                          getBase: GetBase) {
        val newMap = getNewMap()
        putUid(newMap, getUid())
        putHosName(newMap, hos_name)
        putTypeName(newMap, type_name)
        putGradeName(newMap, grade_name)
        putCallTime(newMap, call_time)
        putCallName(newMap, call_name)
        putCallGrade(newMap, call_grade)
        putCallPhone(newMap, call_phone)
        putHosText(newMap, hos_text)
        putFlag(newMap, flag)
        putSign(newMap)
        ApiService.getInstance().api.addHospitalClient(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun addAgentClient(status: Int,
                       name: String,
                       phone: String,
                       area: String,
                       source: String,
                       trade: String,
                       desc: String,
                       flag: String,
                       getBase: GetBase) {
        val newMap = getNewMap()
        putUid(newMap, getUid())
        putStatus(newMap, status.toString())
        putAgentName(newMap, name)
        putPhone(newMap, phone)
        putArea(newMap, area)
        putSource(newMap, source)
        if (trade.isNotEmpty()) {
            putTrade(newMap, trade)
        }
        if (desc.isNotEmpty()) {
            putDesc(newMap, desc)
        }
        putFlag(newMap, flag)
        putSign(newMap)
        ApiService.getInstance().api.addAgentClient(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun getType(getType: GetType) {
        val newMap = getNewMap()
        putSign(newMap)
        ApiService.getInstance().api.getType(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<TypeBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: TypeBean) {
                        getType.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    interface GetType : BaseResponse {
        fun getInfo(type: TypeBean)
    }

    interface GetList : BaseResponse {
        fun getInfo(type: AgentListBean)
    }

    interface GetPotentialCustomers : BaseResponse {
        fun getInfo(info: PotentialCustomerBean)
    }

    interface GetOpenSeaNum : BaseResponse {
        fun getInfo(info: OpenSeaNumBean)
    }

    fun getPotentialCustomers(customer_type: Int, customer_scope: Int, type: String, page: String, num: String, getPotentialCustomers: GetPotentialCustomers) {
        val newMap = getNewMap()
        putPage(newMap, page)
        putNum(newMap, num)
        val observable: Observable<PotentialCustomerBean> = when (type) {
            POTENTIAL_CUSTOMER -> {
                putUserId(newMap)
                putClientType(newMap,customer_type.toString())
                putClientScope(newMap,customer_scope.toString())
                ApiService.getInstance().api.getIntentionClient(newMap)
            }
            C_CUSTOMER -> {
                putUid(newMap)
                ApiService.getInstance().api.getCCustomerList(newMap)
            }
            SIGN_CONTRACT -> {
                putUserId(newMap)
                putClientType(newMap,customer_type.toString())
                putClientScope(newMap,customer_scope.toString())
                ApiService.getInstance().api.getSignContractList(newMap)
            }
            else -> {
                ApiService.getInstance().api.getIntentionClient(newMap)
            }
        }
        putSign(newMap)
        observable
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<PotentialCustomerBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: PotentialCustomerBean) {
                        getPotentialCustomers.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    fun getAgentCustomer(from: String, type: String, page: String, num: String, getList: GetList) {
        val newMap = getNewMap()
        if (from != OPEN_SEA) {
            putId(newMap, getUid())
            putType(newMap, type)
        }
        putPage(newMap, page)
        putNum(newMap, num)
        putSign(newMap)

        val observable: Observable<AgentListBean> = when (from) {
            AGENT -> {
                ApiService.getInstance().api.postAgentList(newMap)
            }
            HOSPITAL -> {
                ApiService.getInstance().api.postHosList(newMap)
            }
            OPEN_SEA -> {
                ApiService.getInstance().api.postOpenSea(newMap)
            }
            else -> {
                ApiService.getInstance().api.postAgentList(newMap)
            }
        }
        observable
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<AgentListBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: AgentListBean) {
                        getList.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        Logger.e(e.toString())
                    }

                })
    }

    fun claimHospitalCustomer(id: String,
                              hos_type: String,
                              hos_grade: String,
                              call_time: String,
                              call_name: String,
                              call_phone: String,
                              call_grade: String,
                              hos_text: String,
                              getBase: GetBase) {
        val newMap = getNewMap()
        putId(newMap, id)
        putUid(newMap)
        putTypeName(newMap, hos_type)
        putGradeName(newMap, hos_grade)
        putCallTime(newMap, call_time)
        putCallName(newMap, call_name)
        putCallPhone(newMap, call_phone)
        putCallGrade(newMap, call_grade)
        putHosText(newMap, hos_text)
        putSign(newMap)
        ApiService.getInstance().api.postClaimHos(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun claimAgentCustomer(id: String,
                           phone: String,
                           area: String,
                           source: String,
                           trade: String,
                           desc: String,
                           getBase: GetBase) {
        val newMap = getNewMap()
        putId(newMap, id)
        putUid(newMap)
        putPhone(newMap, phone)
        putArea(newMap, area)
        putSource(newMap, source)
        putTrade(newMap, trade)
        putDesc(newMap, desc)
        putSign(newMap)
        ApiService.getInstance().api.postClaimAgent(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun getOpenSeaNum(getOpenSeaNum: GetOpenSeaNum) {
        val newMap = getNewMap()
        putSign(newMap)
        ApiService.getInstance().api.getOpenSeaNum(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<OpenSeaNumBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: OpenSeaNumBean) {
                        getOpenSeaNum.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getOpenSeaNum.onError(e.toString())
                    }

                })
    }

    suspend fun searchCustomer(type: String,searchText: String, getCustomerInfo: GetCustomerInfo) {
        val newMap = getNewMap()
        if (type == POTENTIAL_CUSTOMER) {
            putType(newMap,"1")
        } else {
            putType(newMap,"2")
        }
        putUserId(newMap)
        putWhere(newMap,searchText)
        putSign(newMap)
        val searchAsync = BusinessApi.retrofitService.searchAsync(newMap)
        try {
            val info = searchAsync.await()
            getCustomerInfo.getInfo(info)
        } catch (e: Exception) {
            getCustomerInfo.onError(e.toString())
        }
    }

    interface GetCustomerInfo : BaseResponse {
        fun getInfo(info: SearchCustomerBean)
    }
}